package com.systop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systop.entity.SysUser;
import com.systop.dao.SysUserDao;
import com.systop.domain.vo.UsersVo;
import com.systop.exception.PhoneException;
import com.systop.service.SysUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService  {
    @Autowired
    public SysUserDao sysUserDao;

    @Override
    public boolean registerUser(UsersVo usersVo) {
        SysUser users = new SysUser();

        //检查用户手机号是否已经存在
        checkMobileUnique(usersVo.getUserPhone());

        //设置手机号，相当于设置用户账号
        users.setUserCode(usersVo.getUserPhone());
        users.setUserPhone(usersVo.getUserPhone());

        //注册用户随机生成一个用户名
        users.setUserName("用户"+ RandomStringUtils.randomAlphanumeric(10));

        //普通用户注册
        // users.setRole(0);
        //启用状态
        // users.setStatus(1);

        //密码加密
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20); //获取盐
        // System.out.println("加密前:"+usersVo.getUserPass());
        users.setUserPass(new Sha256Hash(usersVo.getUserPass(),salt).toHex());
        // System.out.println("加密后:"+users.getUserPass());
        //将盐存放进入数据库
        users.setSalt(salt);

        return sysUserDao.addUser(users) > 0;
    }

    /**
     * 注册判断用户输入手机号是否存在
     * @param userPhone
     */
    @Override
    public void checkMobileUnique(String userPhone) throws PhoneException {
        Long count = sysUserDao.selectPhone(userPhone);
        if (count>0) {
            throw new PhoneException();
        }
    }

    @Override
    public SysUser findUserByUserCode(String userCode) {
        return baseMapper.selectOne(
                new QueryWrapper<SysUser>().eq("user_code", userCode));
    }

    @Override
    public SysUser findUserByCodeAndPwd(/*String usercode, String userpass*/SysUser sysUser) {
        // SysUser sysUser = sysUserDao.findUserByCodeAndPwd(usercode,userpass);
        SysUser user =sysUserDao.findUserByCodeAndPwd(sysUser);
        return user;
    }

    //查询全部用户信息
    public List<SysUser> findAllUser(SysUser sysUser){
        return sysUserDao.findAllUser(sysUser);
    };
    //查询用户数量
    public int countAllUser(SysUser sysUser){
        return sysUserDao.countAllUser(sysUser);
    };

    //查询收货人
    public int findConsigneeByUserId(SysUser sysUser){
        return sysUserDao.findConsigneeByUserId(sysUser);
    };
    //删除用户
    public int delUser(SysUser sysUser){
        return sysUserDao.delUser(sysUser);
    };

    //添加用户查重
    public int findByUserCode(SysUser sysUser){
        return sysUserDao.findByUserCode(sysUser);
    };
    //添加用户
    public int addUser(SysUser sysUser){
        return sysUserDao.addUser(sysUser);
    }

    //更新用户
    public int udpUser(SysUser sysUser){
        return sysUserDao.udpUser(sysUser);
    };
}
